package presentation;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import controle.ControlJButtonDeleteEtudiant;
import controle.ControlJButtonDeleteEval;
import controle.ControlJButtonDeleteGroupe;
import controle.ControlJButtonDeleteModule;
import controle.ControlJButtonEditEtudiant;
import controle.ControlJButtonEditEval;
import controle.ControlJButtonEditGroupe;
import controle.ControlJButtonEditModule;
import controle.ControlJButtonEditNote;
import controle.ControlJButtonImportNotes;
import controle.ControlJButtonPlusEleve;
import controle.ControlJButtonPlusEval;
import controle.ControlJButtonPlusGroupe;
import controle.ControlJButtonPlusModule;
import controle.ControlJButtonValidation;
import controle.ControlJListEtudiants;
import controle.ControlJListEval;
import controle.ControlJListGroupes;
import controle.ControlJListModules;



import abstraction.Cours;

/** classe qui crée l'affichage principal de notre programme
 * 
 * @author PICOSSON
 *
 */
public class GestionCours extends JFrame {

	private static final long serialVersionUID = 1L;

	private Cours modele;
/**
 * 
 * @param cours
 */
	public GestionCours(Cours cours) {
		super("Gestion Cours");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.modele = cours;
		this.getContentPane().setLayout(new BorderLayout());
		this.creerHaut();
		this.creerBas();
		this.pack();
	}

	private void creerBas() {
		JPanel panelEleves = new JPanel(new BorderLayout());
		panelEleves.setPreferredSize(new Dimension(400, 400));
		panelEleves.setBorder(BorderFactory.createEmptyBorder(3, 6, 6, 6));
		
		JList<String> listeEtudiants = new JList<String>();
		listeEtudiants.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ControlJListEtudiants controlListeEleves = new ControlJListEtudiants(this.modele, listeEtudiants);
		listeEtudiants.addListSelectionListener(controlListeEleves);
		this.modele.addObserver(controlListeEleves);
		
		panelEleves.add(new JLabel("Élève"), BorderLayout.NORTH);
		panelEleves.add(new JScrollPane(listeEtudiants), BorderLayout.CENTER);

		JPanel panelButtonsEleve = new JPanel();
		panelButtonsEleve.add(new JLabel("Élèves :"));
		JButton plusEleve = new JButton(" + ");
		ControlJButtonPlusEleve controleEleve=new ControlJButtonPlusEleve(this, modele);
		plusEleve.addActionListener(controleEleve);
		panelButtonsEleve.add(plusEleve);
		
		JButton moinsEleve = new JButton(" - ");
		ControlJButtonDeleteEtudiant controleDeleteEtudiant=new ControlJButtonDeleteEtudiant(this, modele, moinsEleve);
		moinsEleve.addActionListener(controleDeleteEtudiant);
		this.modele.addObserver(controleDeleteEtudiant);
		panelButtonsEleve.add(moinsEleve);
		
		JButton editEtudiant = new JButton("...");
		ControlJButtonEditEtudiant controleEditEtudiant = new ControlJButtonEditEtudiant(this,modele,editEtudiant);
		editEtudiant.addActionListener(controleEditEtudiant);
		this.modele.addObserver(controleEditEtudiant);
		panelButtonsEleve.add(editEtudiant);
		
		panelButtonsEleve.add(new JLabel("Notes :"));
		JButton editNote = new JButton("...");
		ControlJButtonEditNote controleEditNote = new ControlJButtonEditNote(this, modele, editNote);
		editNote.addActionListener(controleEditNote);
		this.modele.addObserver(controleEditNote);
		panelButtonsEleve.add(editNote);
		
		JButton validation = new JButton(" validation ");
		ControlJButtonValidation controlValidation = new ControlJButtonValidation(this, modele, validation);
		validation.addActionListener(controlValidation);
		this.modele.addObserver(controlValidation);
		panelButtonsEleve.add(validation);
		
		JButton importation = new JButton("import");
		ControlJButtonImportNotes controleImport = new ControlJButtonImportNotes(this,
				modele,importation);
		importation.addActionListener(controleImport);
		this.modele.addObserver(controleImport);
		panelButtonsEleve.add(importation);

		panelEleves.add(panelButtonsEleve, BorderLayout.SOUTH);
		this.getContentPane().add(panelEleves,BorderLayout.CENTER);
	}

	private void creerHaut() {

		JPanel panneauHaut = new JPanel(new GridLayout(1, 3));

		panneauHaut.setPreferredSize(new Dimension(800, 200));

		// Modules
		JPanel panelModules = new JPanel(new BorderLayout());
		panelModules.setBorder(BorderFactory.createEmptyBorder(6, 6, 3, 3));

		JList<String> listeModules = new JList<String>();
		listeModules.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		ControlJListModules controleListeModules = new ControlJListModules(
				this.modele, listeModules);
		listeModules.addListSelectionListener(controleListeModules);
		this.modele.addObserver(controleListeModules);

		panelModules.add(new JScrollPane(listeModules), BorderLayout.CENTER);

		panelModules.add(new JLabel("Module"), BorderLayout.NORTH);
		JPanel panelButtonsModules = new JPanel();
		JButton plusModule = new JButton(" + ");
		ControlJButtonPlusModule controlPlusModule = new ControlJButtonPlusModule(
				this, modele);
		plusModule.addActionListener(controlPlusModule);
		panelButtonsModules.add(plusModule);
		JButton moinsModule = new JButton(" - ");
		ControlJButtonDeleteModule controlDeleteModule = new ControlJButtonDeleteModule(
				this, modele, moinsModule);
		moinsModule.addActionListener(controlDeleteModule);
		modele.addObserver(controlDeleteModule);
		panelButtonsModules.add(moinsModule);
		JButton editModule = new JButton("...");
		ControlJButtonEditModule controlEditModule = new ControlJButtonEditModule(
				this, modele, editModule);
		editModule.addActionListener(controlEditModule);
		modele.addObserver(controlEditModule);
		panelButtonsModules.add(editModule);
		panelModules.add(panelButtonsModules, BorderLayout.SOUTH);
		panneauHaut.add(panelModules);

		// Groupes
		JPanel panelGroupes = new JPanel(new BorderLayout());
		panelGroupes.setBorder(BorderFactory.createEmptyBorder(6, 3, 3, 3));

		JList<String> listeGroupes = new JList<String>();
		listeGroupes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		ControlJListGroupes controleListeGroupes = new ControlJListGroupes(
				this.modele, listeGroupes);
		listeGroupes.addListSelectionListener(controleListeGroupes);
		this.modele.addObserver(controleListeGroupes);

		panelGroupes.add(new JScrollPane(listeGroupes), BorderLayout.CENTER);

		panelGroupes.add(new JLabel("Groupe"), BorderLayout.NORTH);
		JPanel panelButtonsGroupes = new JPanel();
		JButton plusGroupe = new JButton(" + ");
		ControlJButtonPlusGroupe controlPlusGroupe = new ControlJButtonPlusGroupe(
				this, modele, plusGroupe);
		plusGroupe.addActionListener(controlPlusGroupe);
		modele.addObserver(controlPlusGroupe);
		panelButtonsGroupes.add(plusGroupe);
		JButton moinsGroupe = new JButton(" - ");
		ControlJButtonDeleteGroupe controlDeleteGroupe = new ControlJButtonDeleteGroupe(
				this, modele, moinsGroupe);
		moinsGroupe.addActionListener(controlDeleteGroupe);
		modele.addObserver(controlDeleteGroupe);
		panelButtonsGroupes.add(moinsGroupe);
		JButton editGroupe = new JButton("...");
		ControlJButtonEditGroupe controlEditGroupe = new ControlJButtonEditGroupe(
				this, modele, editGroupe);
		editGroupe.addActionListener(controlEditGroupe);
		modele.addObserver(controlEditGroupe);
		panelButtonsGroupes.add(editGroupe);
		panelGroupes.add(panelButtonsGroupes, BorderLayout.SOUTH);
		panneauHaut.add(panelGroupes);
		
		//eval
		JPanel panelEval = new JPanel(new BorderLayout());
		panelEval.setBorder(BorderFactory.createEmptyBorder(6, 6, 3, 3));

		JList<String> listeEval = new JList<String>();
		listeEval.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		ControlJListEval controleListeEvaluations = new ControlJListEval(
				this.modele, listeEval);
		listeEval.addListSelectionListener(controleListeEvaluations);
		this.modele.addObserver(controleListeEvaluations);

		panelEval.add(new JScrollPane(listeEval), BorderLayout.CENTER);

		panelEval.add(new JLabel("Evaluation"), BorderLayout.NORTH);
		
		JPanel panelButtonsEval = new JPanel();
		JButton plusEval = new JButton(" + ");
		ControlJButtonPlusEval controlPlusEval = new ControlJButtonPlusEval(this,modele,plusEval);
		plusEval.addActionListener(controlPlusEval);
		this.modele.addObserver(controlPlusEval);	
		
		panelButtonsEval.add(plusEval);
		
		JButton moinsEval = new JButton(" - ");
		ControlJButtonDeleteEval controlDeleteEval = new ControlJButtonDeleteEval(
				this, modele, moinsEval);
		moinsEval.addActionListener(controlDeleteEval);
		modele.addObserver(controlDeleteEval);
		panelButtonsEval.add(moinsEval);
		
		JButton editEval = new JButton("...");
		ControlJButtonEditEval controlEditEval = new ControlJButtonEditEval(
				this, modele, editEval);
		editEval.addActionListener(controlEditEval);
		modele.addObserver(controlEditEval);		
		panelButtonsEval.add(editEval);
		
		panelEval.add(panelButtonsEval, BorderLayout.SOUTH);
		panneauHaut.add(panelEval);

		this.getContentPane().add(panneauHaut,BorderLayout.NORTH);
	}

	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		Cours modules = new Cours();

		GestionCours fen = new GestionCours(modules);
		fen.setVisible(true);
	}

}
